﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XiaoGang.Soft.XGWMS.COM;
using XiaoGang.Soft.XGWMS.MODEL;

namespace XiaoGang.Soft.XGWMS.DAL_SQL
{
    public class DAL_M014_BILLSCAN
    {
        /// <summary>	
        /// <p><b>方法名称</b>SearchGoodsDetail</p>
        /// <p><b>概要説明  :</b>商品信息的详细查询</p>
        /// <p><b>作成者	:</b>万岗</p>
        /// <p><b>作成日	:</b>2008/09/08</p>
        /// <p><b>修改历史  :</b></p>
        /// </summary>
        /// <param type="string" param="strBillID">货单编号</param>
        /// <returns type="ReturnValue">查询结果</returns>
        public ReturnValue SearchGoodsDetail(string strBillID)
        {
            SqlParameter parameter = 
                new SqlParameter("@BILL_ID", SqlDbType.Char, 12);
            parameter.Value = strBillID.Trim();

            return XGWMS.DBUtility.SqlHelper.Query(DAL_M014_BILLSCAN_SQL.Search_Goods_Detail_SQL, parameter);
        }

        /// <summary>	
        /// <p><b>方法名称</b>SearchStorageDetail</p>
        /// <p><b>概要説明  :</b>商品进出库流水信息查询</p>
        /// <p><b>作成者	:</b>万岗</p>
        /// <p><b>作成日	:</b>2010/02/25</p>
        /// <p><b>修改历史  :</b></p>
        /// </summary>
        /// <param type="string" param="strGoodID">商品编号</param>
        /// <returns type="ReturnValue">查询结果</returns>
        public ReturnValue SearchStorageDetail(string strGoodID)
        {
            SqlParameter parameter = new SqlParameter("@GOODS_ID", SqlDbType.Char, 12);
            parameter.Value = strGoodID.Trim();
            return XGWMS.DBUtility.SqlHelper.Query(DAL_M014_BILLSCAN_SQL.SearchStorageDetail_SQL, parameter);
        }

        /// <summary>
        /// 取得商品[进][出][总]合计
        /// </summary>
        /// <param name="strGoodID"></param>
        /// <returns></returns>
        public ReturnValue GetAllUnits(string strGoodID)
        {
            SqlParameter parameter = new SqlParameter("@GOODS_ID", SqlDbType.Char, 12);
            parameter.Value = strGoodID.Trim();
            return XGWMS.DBUtility.SqlHelper.Query(DAL_M014_BILLSCAN_SQL.GetAllUnits_SQL, parameter);
        }

        /// <summary>
        /// 取得盘库商品
        /// </summary>
        /// <param name="strBill_ID">盘库编号</param>
        /// <param name="strStocktakingFlag">盘库标志:0=已盘+未盘 1=已盘 2=未盘</param>
        /// <param name="modGoods">商品过滤条件</param>
        /// <returns></returns>
        public ReturnValue GetStocktakingGoods(string strBill_ID, string strStocktakingFlag,MOD_M007_GOODS modGoods)
        {
            string strWhere=string.Empty;
            if(strStocktakingFlag=="1")
            {
                strWhere+=" and isnull(SGoods.BILL_ID,'') <>'' ";
            }
            else if(strStocktakingFlag=="2")
            {
                strWhere += " and isnull(SGoods.BILL_ID,'') = '' ";
            }
            if (modGoods.GOODS_NO.Trim().Length > 0)
            {
                strWhere += " and (M007.GOODS_NO = '" + modGoods.GOODS_NO.Trim () + "' Or M007.Goods_ID IN (Select GoodsID from M013_GoodSNo Where GoodsNo = '" + modGoods.GOODS_NO.Trim () + "') )";
            }
            if (modGoods.GOODS_NM1.Trim().Length > 0)
            {
                strWhere += " and M007.GOODS_NM1 Like '%" + modGoods.GOODS_NM1.Trim() + "%' ";
            }
            if (modGoods.GOODS_GROUP.Trim().Length > 0)
            {
                strWhere += " and M007.GOODS_GROUP = '" + modGoods.GOODS_GROUP.Trim() + "' ";
            }
            SqlParameter[] parameters = {new SqlParameter("@BILL_ID", SqlDbType.Char, 12),
                new SqlParameter("@STOREROOM_ID", SqlDbType.Char, 12)};
            parameters[0].Value = strBill_ID;
            parameters[1].Value = modGoods.StoreRoom_ID;
            return XGWMS.DBUtility.SqlHelper.Query(DAL_M014_BILLSCAN_SQL.GetStocktakingGoods_SQL.Replace("@@@", strWhere), parameters);
        }

        public ReturnValue GetLastStocktakingID()
        {
            return XGWMS.DBUtility.SqlHelper.Query(DAL_M010_BILL_SQL.Get_LastStocktakingID_SQL);
        }
    }
}
